package com.shujia.service;

import com.shujia.bean.User;
import com.shujia.dao.UserDao;
import com.shujia.dao.UserDaoImpl;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public String login(String username, String password) {


        //判断用户名是否存在
        User user = userDao.queryUserByUsername(username);

        if (user == null) {
            return "用户名不正确";
        }

        //判断密码是否正确
        if (password == null || !password.equals(user.getPassword())) {
            return "密码不对";
        }

        return "登录成功";


    }

    @Override
    public String register(String username, String password, String nowPassword) {


        //判断密码是否一致
        if (password == null || !password.equals(nowPassword)) {
            return "密码不一致";
        }

        //判断用户名是否存在
        User user = userDao.queryUserByUsername(username);

        if (user != null) {
            return "用户已存在";
        }

        //将新用户保存到数据库
        User newUser = new User(username, password);

        userDao.insert(newUser);


        return "注册成功";
    }

    @Override
    public String modifyPassword(String username, String oldPassword, String newPassword, String nowPassword) {
        return null;
    }
}
